<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="web安全,XSS,跨站脚本攻击," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="XSS攻击，通常指黑客通过“html注入”篡改了网页，插入了恶意的脚本，从而在用户浏览网页的时候，控制用户浏览器的一种攻击。其原理是攻击者向有XSS漏洞的网站中输入(传入)一段恶意的HTML代码，当其它用户浏览该网站时，这段HTML代码会自动执行，从而达到攻击的目的。如，盗取用户Cookie、破坏页面结构、重定向到其它网站等。
XSS攻击的生效方式（按危害大小排序）：1）构造URLXSS攻击者通过">
<meta property="og:type" content="article">
<meta property="og:title" content="web安全（1）-- XSS（跨站脚本攻击）">
<meta property="og:url" content="http://www.taneroom.cn/2016/11/25/web安全（1）-XSS（跨站脚本攻击）/index.html">
<meta property="og:site_name" content="TaneRoom">
<meta property="og:description" content="XSS攻击，通常指黑客通过“html注入”篡改了网页，插入了恶意的脚本，从而在用户浏览网页的时候，控制用户浏览器的一种攻击。其原理是攻击者向有XSS漏洞的网站中输入(传入)一段恶意的HTML代码，当其它用户浏览该网站时，这段HTML代码会自动执行，从而达到攻击的目的。如，盗取用户Cookie、破坏页面结构、重定向到其它网站等。
XSS攻击的生效方式（按危害大小排序）：1）构造URLXSS攻击者通过">
<meta property="og:image" content="http://www.taneroom.cn/images/2016/11/25/xss.png">
<meta property="og:image" content="http://www.taneroom.cn/images/2016/11/25/xss1.png">
<meta property="og:image" content="http://www.taneroom.cn/images/2016/11/25/20161101094550530.png">
<meta property="og:updated_time" content="2016-11-25T08:27:13.199Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="web安全（1）-- XSS（跨站脚本攻击）">
<meta name="twitter:description" content="XSS攻击，通常指黑客通过“html注入”篡改了网页，插入了恶意的脚本，从而在用户浏览网页的时候，控制用户浏览器的一种攻击。其原理是攻击者向有XSS漏洞的网站中输入(传入)一段恶意的HTML代码，当其它用户浏览该网站时，这段HTML代码会自动执行，从而达到攻击的目的。如，盗取用户Cookie、破坏页面结构、重定向到其它网站等。
XSS攻击的生效方式（按危害大小排序）：1）构造URLXSS攻击者通过">
<meta name="twitter:image" content="http://www.taneroom.cn/images/2016/11/25/xss.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://www.taneroom.cn/2016/11/25/web安全（1）-XSS（跨站脚本攻击）/"/>





  <title> web安全（1）-- XSS（跨站脚本攻击） | TaneRoom </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?91ca0f0e00c720ad3b92033fd9766f4b";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">TaneRoom</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">低调的码农</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://www.taneroom.cn/2016/11/25/web安全（1）-XSS（跨站脚本攻击）/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="TaneRoom">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="/images/avatar.png">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="TaneRoom">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="TaneRoom" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                web安全（1）-- XSS（跨站脚本攻击）
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2016-11-25T13:04:01+08:00">
                2016-11-25
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/web安全/" itemprop="url" rel="index">
                    <span itemprop="name">web安全</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <a href="/2016/11/25/web安全（1）-XSS（跨站脚本攻击）/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/11/25/web安全（1）-XSS（跨站脚本攻击）/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          

          
          
             <span id="/2016/11/25/web安全（1）-XSS（跨站脚本攻击）/" class="leancloud_visitors" data-flag-title="web安全（1）-- XSS（跨站脚本攻击）">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               <span class="post-meta-item-text">阅读次数 </span>
               <span class="leancloud-visitors-count"></span>
              </span>
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>XSS攻击，通常指黑客通过“html注入”篡改了网页，插入了恶意的脚本，从而在用户浏览网页的时候，控制用户浏览器的一种攻击。其原理是攻击者向有XSS漏洞的网站中输入(传入)一段恶意的HTML代码，当其它用户浏览该网站时，这段HTML代码会自动执行，从而达到攻击的目的。如，盗取用户Cookie、破坏页面结构、重定向到其它网站等。</p>
<h2 id="XSS攻击的生效方式（按危害大小排序）："><a href="#XSS攻击的生效方式（按危害大小排序）：" class="headerlink" title="XSS攻击的生效方式（按危害大小排序）："></a>XSS攻击的生效方式（按危害大小排序）：</h2><h3 id="1）构造URL"><a href="#1）构造URL" class="headerlink" title="1）构造URL"></a>1）构造URL</h3><p>XSS攻击者通过构造URL的方式构造了一个有问题的页面；当其他人点击了此页面后，会发现页面出错，或者被暗中执行了某些js脚本，这时，攻击行为才真正生效。<br>一般来说，动态页面中会将url中的部分内容回写在页面中。以百度的搜索为例：<br><img src="http://www.taneroom.cn/images/2016/11/25/xss.png" alt="XSS注入" title="XSS注入"><br>搜索结果页中，会在标题中和搜索框中回写用户输入的内容。从页面的源代码中，我们看到，这两处实际也经过了转义处理，变成了&lt;script&gt;alert(&quot;wrong&quot;)&lt;/script&gt;显示。如果这里没有经过转义处理，则页面中就嵌入了一段script，会执行，并弹出对话框提示用户。如果是其他恶意代码，则可能造成破坏。转义如下图：<br><img src="http://www.taneroom.cn/images/2016/11/25/xss1.png" alt="百度针对XSS的转义" title="百度针对XSS的转义"><br>然后如果攻击者将此URL广为传播——比如说，以报错的方式发给百度的管理员，管理员打开这个URL就中招了。</p>
<h3 id="2）发布内容式"><a href="#2）发布内容式" class="headerlink" title="2）发布内容式"></a>2）发布内容式</h3><p>构造URL攻击方式传播范围有限，被攻击者只要有基本的安全意识就可以避免，因此这种手段的危险性比较小。相比之下，通过发表内容构造的XSS的危害就大了很多。<br>在任何可以发表内容的论坛、讨论区、贴吧、博客、微博等网站上，用户发表的内容会保存起来，允许其他用户浏览。这些保存的内容显示在页面上的时候，如果没有经过正确的处理，也会把攻击者精心构造的内容显示出来，访问该内容的用户就此中招。如果该页面流传广泛，则影响会更加深远。<br>一般来说，这种攻击都做得比较隐蔽，被攻击者并不知道自己什么时候踩中了地雷，网站也很难追查问题的来源。</p>
<h3 id="3）蠕虫式"><a href="#3）蠕虫式" class="headerlink" title="3）蠕虫式"></a>3）蠕虫式</h3><p>上述两种方式的流传范围都有一定限度；最彻底最暴力的方式是使用蠕虫–就是首先发一个有问题的文章，浏览者阅读时会被暗中执行恶意代码，发表一篇新的文章的，该文章也含有同样的恶意代码。这样有可能在最快时间内将攻击铺满整个网站。<br>下面是6月28日新浪微博被蠕虫攻击的报告，其实质就是XSS攻击。<br><a href="http://www.ijinshan.com/news/20110629001.shtml" target="_blank" rel="external">http://www.ijinshan.com/news/20110629001.shtml</a></p>
<h2 id="XSS攻击的防范方式："><a href="#XSS攻击的防范方式：" class="headerlink" title="XSS攻击的防范方式："></a>XSS攻击的防范方式：</h2><p>防范XSS攻击的方式也十分简单：转义！但是你会面临两种抉择，是在入库前转义呢，还是在读取前转义呢？<br>这两种方式各有优劣，对于新手来说，一般会选择入库前转义，因为这种方法看起来可以一劳永逸，而且操作非常简单，只需要写一个filter或者interceptor拦截所有的写入请求，统一转义后交给controller处理入库即可。但是如果你的终端是多样化的，需要有多种展现形式的时候，这种方式就显得力不从心了。比如说你的终端要求以XML的形式显示数据，那么你就先得unescape回原始数据然后再按照xml的格式escape一次。假如终端要求更复杂的方式，转换起来就更复杂了。所以老手们慢慢会选择使用第二种方式。<br>我来总结一下一下这两种方式的优缺点：</p>
<table>
<thead>
<tr>
<th>转义方式</th>
<th>优点</th>
<th>缺点</th>
</tr>
</thead>
<tbody>
<tr>
<td>入库前转义</td>
<td>一劳永逸</td>
<td>需要针对多端进行不同的输出，灵活性不足，无法应对后期数据格式的变化</td>
</tr>
<tr>
<td>读取前转义</td>
<td>简单，灵活，能应对各种数据格式的场景</td>
<td>需要对每个输出数据转义，人工处理容易遗漏</td>
</tr>
</tbody>
</table>
<p>本人推荐第二种方式来防范xss攻击。虽然需要对每个输出数据都进行转义，但是如果你使用带自动转义的模版或者框架来处理的话，那么就可以极大的提高效率，又可以规避安全的问题。<br>下面介绍两种基础转义的方法：<br>1）使用Apache的commons-lang.jar<br><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">StringEscapeUtils.escapeHtml(str);// 汉字会转换成对应的ASCII码，空格不转换</div></pre></td></tr></table></figure></p>
<p>2）自己实现转换，只转换部分字符<br><figure class="highlight d"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">private</span> <span class="keyword">static</span> String htmlEncode(charc) &#123;</div><div class="line">    <span class="keyword">switch</span>(c) &#123;</div><div class="line">        <span class="keyword">case</span><span class="string">'&amp;'</span>:</div><div class="line">            <span class="keyword">return</span><span class="string">"&amp;"</span>;</div><div class="line">        <span class="keyword">case</span><span class="string">'&lt;'</span>:</div><div class="line">            <span class="keyword">return</span><span class="string">"&lt;"</span>;</div><div class="line">        <span class="keyword">case</span><span class="string">'&gt;'</span>:</div><div class="line">            <span class="keyword">return</span><span class="string">"&gt;"</span>;</div><div class="line">        <span class="keyword">case</span><span class="string">'"'</span>:</div><div class="line">            <span class="keyword">return</span><span class="string">""</span><span class="string">";</span></div><div class="line">        case' ':</div><div class="line">            return" <span class="string">";</span></div><div class="line">        default:</div><div class="line">            returnc +"<span class="string">";</span></div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 对传入的字符串str进行Html encode转换</div><div class="line"> */</div><div class="line">public static String htmlEncode(String str) &#123;</div><div class="line">    if(str ==null|| str.trim().equals("<span class="string">"))returnstr;</span></div><div class="line">    StringBuilder encodeStrBuilder =newStringBuilder();</div><div class="line">    for(inti =0, len = str.length(); i &lt; len; i++) &#123;</div><div class="line">        encodeStrBuilder.append(htmlEncode(str.charAt(i)));</div><div class="line">    &#125;</div><div class="line">    returnencodeStrBuilder.toString();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h2 id="另外介绍一种最常见的XSS攻击及其防范方法"><a href="#另外介绍一种最常见的XSS攻击及其防范方法" class="headerlink" title="另外介绍一种最常见的XSS攻击及其防范方法"></a>另外介绍一种最常见的XSS攻击及其防范方法</h2><p>最常见的XSS攻击就是通过读取浏览器的Cookie对象，从而发起“cookie劫持”，当前用户的登录凭证存储于服务器的session中，而在浏览器中是以cookie的形式进行存储的，cookie被劫持后，意味着攻击者可以不通过密码而直接登录系统。我们也可以直接在浏览器中输入脚本javaScript:alert(document.cookie)来获取当前cookie值。<br>目前防止“cookie劫持”的方法大致有：<br>    a. 输入检查，使用filter来过滤敏感的关键字；<br>    b. 将cookie与用户ip地址进行绑定；<br>    c. 为cookie植入HttpOnly标识。<br>本方法主要介绍第3种方式：为cookie植入HttpOnly标识。一旦这个HttpOnly被设置，你在浏览器的document对象中就看不到Cookie了，而浏览器在浏览的时候不受任何影响，因为Cookie会被放在浏览器头中发送出去(包括ajax的时候)，应用程序也一般不会在js里操作这些敏感Cookie的，对于一些敏感的Cookie我们采用HttpOnly，对于一些需要在应用程序中用js操作的cookie我们就不予设置，这样就保障了Cookie信息的安全也保证了应用。<br>具体代码实现：在web服务器tomcat的配置文件server.xml中添加：<br><figure class="highlight nix"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;Context <span class="attr">docBase="E:\tomcat\apache-tomcat-6.0.24/webapps/netcredit"</span> <span class="attr">path="/netcredit"</span> <span class="attr">reloadable="false"</span> <span class="attr">useHttpOnly="true"/&gt;</span></div></pre></td></tr></table></figure></p>
<p>或者在项目的web.xml 配置如下：<br><figure class="highlight vbscript-html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;session-config&gt;</div><div class="line">&lt;cookie-config&gt;</div><div class="line">&lt;http-only&gt;true&lt;/http-only&gt;</div><div class="line">&lt;/cookie-config&gt;</div><div class="line">&lt;/session-config&gt;</div></pre></td></tr></table></figure></p>
<p>浏览器cookie截图<br><img src="http://www.taneroom.cn/images/2016/11/25/20161101094550530.png" alt="HttpOnly" title="HttpOnly"><br>PS：<a href="http://blog.csdn.net/a19881029/article/details/27536917" target="_blank" rel="external">Cookie设置HttpOnly，Secure，Expire属性</a></p>
<p><a href="http://blog.csdn.net/a19881029/article/details/27536917" target="_blank" rel="external">http://blog.csdn.net/a19881029/article/details/27536917</a></p>

      
    </div>

    <div>
      
        
<div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center">
    <img id="wechat_subscriber_qcode" src="/images/donate/wechat.png" alt="TaneRoom wechat" style="width: 200px; max-width: 100%;"/>
    <div>请博主喝一杯咖啡！</div>
</div>


      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/web安全/" rel="tag"># web安全</a>
          
            <a href="/tags/XSS/" rel="tag"># XSS</a>
          
            <a href="/tags/跨站脚本攻击/" rel="tag"># 跨站脚本攻击</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/11/24/hello-world/" rel="next" title="Hello World">
                <i class="fa fa-chevron-left"></i> Hello World
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/11/25/web安全（2）-CSRF（跨站点请求伪造）/" rel="prev" title="web安全（2）-- CSRF（跨站点请求伪造）">
                web安全（2）-- CSRF（跨站点请求伪造） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <div class="ds-share flat" data-thread-key="2016/11/25/web安全（1）-XSS（跨站脚本攻击）/"
     data-title="web安全（1）-- XSS（跨站脚本攻击）"
     data-content=""
     data-url="http://www.taneroom.cn/2016/11/25/web安全（1）-XSS（跨站脚本攻击）/">
  <div class="ds-share-inline">
    <ul  class="ds-share-icons-16">

      <li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
      <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
      <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
      <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
      <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>

    </ul>
    <div class="ds-share-icons-more">
    </div>
  </div>
</div>
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/11/25/web安全（1）-XSS（跨站脚本攻击）/"
           data-title="web安全（1）-- XSS（跨站脚本攻击）" data-url="http://www.taneroom.cn/2016/11/25/web安全（1）-XSS（跨站脚本攻击）/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.png"
               alt="TaneRoom" />
          <p class="site-author-name" itemprop="name">TaneRoom</p>
          <p class="site-description motion-element" itemprop="description">低调的码农</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">11</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">2</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">12</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/u/2711247437" target="_blank" title="Weibo">
                  
                    <i class="fa fa-fw fa-weibo"></i>
                  
                  Weibo
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://blog.csdn.net/tanzhen1991910" target="_blank" title="CSDN">
                  
                    <i class="fa fa-fw fa-book"></i>
                  
                  CSDN
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#XSS攻击的生效方式（按危害大小排序）："><span class="nav-number">1.</span> <span class="nav-text">XSS攻击的生效方式（按危害大小排序）：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1）构造URL"><span class="nav-number">1.1.</span> <span class="nav-text">1）构造URL</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2）发布内容式"><span class="nav-number">1.2.</span> <span class="nav-text">2）发布内容式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3）蠕虫式"><span class="nav-number">1.3.</span> <span class="nav-text">3）蠕虫式</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#XSS攻击的防范方式："><span class="nav-number">2.</span> <span class="nav-text">XSS攻击的防范方式：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#另外介绍一种最常见的XSS攻击及其防范方法"><span class="nav-number">3.</span> <span class="nav-text">另外介绍一种最常见的XSS攻击及其防范方法</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">TaneRoom</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"taneroom"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  








  
  

  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("BnRa0PjEwPETnK8UggnNxrvQ-gzGzoHsz", "gTNTrFanjWAJhrkpXA6VDjbf");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  


</body>
</html>
